/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MusicGroup_Album_Management;


//import Artist_Album_Management.ArtistAlbumTable;
//import Main_menu.MainMenu;
import Radiostation_POJOS.Song;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


public class MusicGroupAlbumManagementForm extends javax.swing.JFrame {

    DefaultTableModel TableModel = new DefaultTableModel();
    Song song;
    List<Song> SongsList = new ArrayList<Song>();
    int selectedRow = 0;
    boolean flag;
    boolean checkTitle;
    
    //Μέθοδος για την εισαγωγή των στοιχείων του επιλεγμένου Άλπμουμ στα πεδία της φόρμας.
    public void InsertValuesToForm(){
        
                try{
        
            String url = "jdbc:derby://localhost:1527/Radiostation_DB";
            String username = "erg3";
            String password = "erg3";

            Connection con = DriverManager.getConnection(url, username, password);
            Statement stat = con.createStatement();
            
            String Query1 = "SELECT * FROM ALBUM JOIN MUSICGROUP ON ALBUM.MUSICGROUP = MUSICGROUP.MUSICGROUP_ID WHERE TITLE = '"+MusicGroupsAlbumArchive.albumTitle+"'";
            String Query2 = "SELECT * FROM SONG JOIN ALBUM ON SONG.ALBUM_ID = ALBUM.ALBUM_ID WHERE ALBUM.TITLE = '"+MusicGroupsAlbumArchive.albumTitle+"'";            
            
            ResultSet rs = stat.executeQuery(Query1);
            
            while (rs.next()){
                
                TitleTxt0.setText(rs.getString(2));
                AlbumTypeCombo.setSelectedItem(rs.getString(4));
                DiskNumberTxt2.setText(rs.getString(5));
                CompanyNameTxt3.setSelectedItem(rs.getString(8));
                ReleaseDateTxt4.setText(rs.getString(3));
                GroupNameTxt5.setSelectedItem(rs.getString("NAME"));
                
            }
            
            ResultSet rs2 = stat.executeQuery(Query2);
            SongsTable.setModel(TableModel);
            
            while (rs2.next()){
                Object [] ob = {rs2.getString("TITLE") , rs2.getString("DURATION") , rs2.getInt("TRACKNR")};
                TableModel.addRow(ob);
                String Title = rs2.getString("TITLE");
                String Duration = rs2.getString("DURATION");
                int Number = rs2.getInt("TRACKNR");
                song = new Song();
                song.setTitle(Title);
                song.setDuration(Duration);
                song.setTracknr(Number);
                songList.add(song);
            }            
        }
        
        catch(SQLException ex){
            JOptionPane.showMessageDialog(null, ex.toString());
            }
        
    }
    
    
    private void FillComboCompanyName(){
        try{
            String url = "jdbc:derby://localhost:1527/Radiostation_DB";
            String username = "erg3";
            String password = "erg3";

            Connection con = DriverManager.getConnection(url, username, password);
            Statement stat = con.createStatement();
            String Query2 = "SELECT * FROM MUSICPRODUCTIONCOMPANY";
            ResultSet rs = stat.executeQuery(Query2);
            
            while(rs.next()){
                String name = rs.getString("NAME");
                CompanyNameTxt3.addItem(name);
            }
        }
        
        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
    }
    
    
    private void FillComboGroupName(){
        try{
            String url = "jdbc:derby://localhost:1527/Radiostation_DB";
            String username = "erg3";
            String password = "erg3";

            Connection con = DriverManager.getConnection(url, username, password);
            Statement stat = con.createStatement();
            String Query = "SELECT * FROM MUSICGROUP";
            ResultSet rs = stat.executeQuery(Query);
            
            while(rs.next()){
                String name = rs.getString("NAME");
                GroupNameTxt5.addItem(name);
            }
        }
        
        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
    }
    
    
    public MusicGroupAlbumManagementForm() {
        initComponents();
        setLocationRelativeTo(null);//για την εμφάνιση του παραθύρου στο κέντρο
        FillComboCompanyName();
        FillComboGroupName();
        SongsTable.setModel(TableModel);
        TableModel.addColumn("ΤΙΤΛΟΣ");
        TableModel.addColumn("ΔΙΑΡΚΕΙΑ");
        TableModel.addColumn("ΑΡΙΘΜΟΣ ΣΕΙΡΑΣ");
        songList.clear();
        flag = false;        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        Radiostation_ProjectPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("Radiostation_ProjectPU").createEntityManager();
        artistQuery = java.beans.Beans.isDesignTime() ? null : Radiostation_ProjectPUEntityManager.createQuery("SELECT a FROM Artist a");
        artistList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : artistQuery.getResultList();
        musicproductioncompanyQuery = java.beans.Beans.isDesignTime() ? null : Radiostation_ProjectPUEntityManager.createQuery("SELECT m FROM Musicproductioncompany m");
        musicproductioncompanyList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : musicproductioncompanyQuery.getResultList();
        songQuery = java.beans.Beans.isDesignTime() ? null : Radiostation_ProjectPUEntityManager.createQuery("SELECT s FROM Song s");
        songList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : songQuery.getResultList();
        songQuery1 = java.beans.Beans.isDesignTime() ? null : Radiostation_ProjectPUEntityManager.createQuery("SELECT s FROM Song s");
        songList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : songQuery1.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TitleTxt0 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        DiskNumberTxt2 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        ReleaseDateTxt4 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        SaveBtn = new javax.swing.JButton();
        CancelBtn = new javax.swing.JButton();
        InsertSong = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        SongTitle = new javax.swing.JTextField();
        Song = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        SongDuration = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        SongNumber = new javax.swing.JTextField();
        GroupNameTxt5 = new javax.swing.JComboBox();
        CompanyNameTxt3 = new javax.swing.JComboBox();
        AlbumTypeCombo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        SongsTable = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Διαχείριση Άλμπουμ Συγκροτήματος");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Τύπος Άλμπουμ:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Τίτλος:");

        TitleTxt0.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        TitleTxt0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TitleTxt0ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Αριθμός Άλμπουμ:");

        DiskNumberTxt2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        DiskNumberTxt2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DiskNumberTxt2ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Εταιρεία Παραγωγής:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Ημερ. Κυκλοφορίας:");

        ReleaseDateTxt4.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ReleaseDateTxt4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReleaseDateTxt4ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Συγκρότημα:");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel8.setText("Λίστα Τραγουδιών:");

        SaveBtn.setText("Αποθήκευση");
        SaveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveBtnActionPerformed(evt);
            }
        });

        CancelBtn.setText("Ακύρωση");
        CancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelBtnActionPerformed(evt);
            }
        });

        InsertSong.setText("Εισαγωγή Τραγουδιού");
        InsertSong.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertSongActionPerformed(evt);
            }
        });

        jButton2.setText("Διαγραφή Τραγουδιού");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteSongActionPerformed(evt);
            }
        });

        Song.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        Song.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Song.setText("Εισαγωγή Στοιχείων Ταγουδιού:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5.setText("Τίτλος");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel9.setText("Διάρκεια");

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel10.setText("Αριθμός Σειράς");

        GroupNameTxt5.setRequestFocusEnabled(false);

        AlbumTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "CD_SINGLE(CS)", "EXTENDED_PLAY(EP)", "LONG_PLAY(LP)" }));

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, songList, SongsTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${title}"));
        columnBinding.setColumnName("ΤΙΤΛΟΣ");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duration}"));
        columnBinding.setColumnName("ΔΙΑΡΚΕΙΑ");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${tracknr}"));
        columnBinding.setColumnName("ΑΡΙΘΜΟΣ ΣΕΙΡΑΣ");
        columnBinding.setColumnClass(Integer.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(SongsTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(216, 216, 216)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(SongTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                                .addGap(42, 42, 42)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addComponent(SongDuration, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(20, 20, 20)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(SongNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel10)))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(InsertSong)
                            .addComponent(jButton2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(Song)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(SaveBtn)
                                .addComponent(jLabel8)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(ReleaseDateTxt4, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(TitleTxt0)
                            .addComponent(GroupNameTxt5, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(CompanyNameTxt3, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(DiskNumberTxt2)
                            .addComponent(AlbumTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(742, 742, 742)
                        .addComponent(CancelBtn)))
                .addGap(87, 87, 87))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TitleTxt0, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(AlbumTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DiskNumberTxt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CompanyNameTxt3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ReleaseDateTxt4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GroupNameTxt5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SongNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(SongDuration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(Song)
                                    .addComponent(InsertSong))
                                .addGap(36, 36, 36))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(SongTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)))))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)
                    .addComponent(jLabel8)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(SaveBtn)
                            .addComponent(CancelBtn))))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TitleTxt0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TitleTxt0ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TitleTxt0ActionPerformed

    private void DiskNumberTxt2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DiskNumberTxt2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_DiskNumberTxt2ActionPerformed

    private void ReleaseDateTxt4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReleaseDateTxt4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ReleaseDateTxt4ActionPerformed

    private void SaveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveBtnActionPerformed
        if (MusicGroupsAlbumArchive.FLAG2 == true){//εάν καταχωρείτε νέο Άλμπουμ.
            if(songList.size()>1){//έλεγχος εάν έχουν καταχωρηθεί τουλάχιστον δύο τραγούδια στο Άλμπουμ.            
                try {
                    String url = "jdbc:derby://localhost:1527/Radiostation_DB";
                    String username = "erg3";
                    String password = "erg3";

                    Connection con = DriverManager.getConnection(url, username, password);
                    Statement stat = con.createStatement();
                    String Query3 = "INSERT INTO ALBUM (TITLE, TYPE, DISCNUMBER, MUSICPRODUCTIONCOMPANYNAME, RELEASEDATE, MUSICGROUP) VALUES ('"+TitleTxt0.getText()+"' , '"+AlbumTypeCombo.getSelectedItem()+"' , '"+DiskNumberTxt2.getText()+"' , '"+CompanyNameTxt3.getSelectedItem()+"' , '"+ReleaseDateTxt4.getText()+"' , (SELECT MUSICGROUP_ID FROM MUSICGROUP WHERE MUSICGROUP.NAME = '"+GroupNameTxt5.getSelectedItem()+"'))";

                    stat.execute(Query3);
                    //JOptionPane.showMessageDialog(null, "Μέγεθος songList: " + songList.size());                

                    for(int count1 = 0; count1<songList.size(); count1++){
                        String Query4 = "INSERT INTO SONG (TITLE , DURATION , TRACKNR , ALBUM_ID) VALUES ('"+songList.get(count1).getTitle()+"' , '"+songList.get(count1).getDuration()+"' , "+songList.get(count1).getTracknr()+" , (SELECT ALBUM_ID FROM ALBUM WHERE TITLE = '"+TitleTxt0.getText()+"'))";
                        stat.execute(Query4);
                    }


                    JOptionPane.showMessageDialog(null, "Επιτυχής καταχώρηση Άλμπουμ ");
                    }

                catch(SQLException ex){
                JOptionPane.showMessageDialog(null, ex.toString());
                }


                new MusicGroupsAlbumArchive().setVisible(true);
                dispose();
            }
            else
                   JOptionPane.showMessageDialog(null, "Το Άλμπουμ πρέπει να περιέχει τουλάχιστον δύο τραγούδια"); 
            
        }
        else{//αλλιώς εάν επεξεργάζετε κάποιο Άλμπουμ.
            if(songList.size()>1){//έλεγχος εάν έχουν καταχωρηθεί τουλάχιστον δύο τραγούδια στο Άλμπουμ.            
                try {
                    String url = "jdbc:derby://localhost:1527/Radiostation_DB";
                    String username = "erg3";
                    String password = "erg3";

                    Connection con = DriverManager.getConnection(url, username, password);
                    Statement stat = con.createStatement();
                    //Query για την ανανέωση των στοιχείων στον πίνακα "Artist" της επιλεγμένης εγγραφής Άλμπουμ στο JTable "ArtistsTable". 
                    String Query5 = "UPDATE ALBUM SET TITLE = '"+TitleTxt0.getText()+"' , TYPE = '"+AlbumTypeCombo.getSelectedItem()+"' , DISCNUMBER = '"+DiskNumberTxt2.getText()+"' , MUSICPRODUCTIONCOMPANYNAME =  '"+CompanyNameTxt3.getSelectedItem()+"' , RELEASEDATE = '"+ReleaseDateTxt4.getText()+"' , MUSICGROUP = (SELECT MUSICGROUP_ID FROM MUSICGROUP WHERE MUSICGROUP.NAME ='"+GroupNameTxt5.getSelectedItem()+"') WHERE TITLE = '"+MusicGroupsAlbumArchive.albumTitle+"'";

                    stat.execute(Query5);

                    if(flag == true){
                        String Query2 = "DELETE FROM SONG WHERE ALBUM_ID = (SELECT ALBUM_ID FROM ALBUM WHERE TITLE = '"+MusicGroupsAlbumArchive.albumTitle+"')";

                        stat.execute(Query2);

                        for(int count2 = 0; count2<songList.size(); count2++){
                        String Query3 = "INSERT INTO SONG (TITLE , DURATION , TRACKNR , ALBUM_ID) VALUES ('"+songList.get(count2).getTitle()+"' , '"+songList.get(count2).getDuration()+"' , "+songList.get(count2).getTracknr()+" , (SELECT ALBUM_ID FROM ALBUM WHERE TITLE = '"+TitleTxt0.getText()+"'))";

                        stat.execute(Query3);
                        }
                    }


                    //Μήνυμα για την επιτυχή ανανέωση των στοιχείων.
                    JOptionPane.showMessageDialog(null, "Επιτυχής ανανέωση στοιχείων Άλμπουμ ");
                }

                catch(SQLException ex){
                JOptionPane.showMessageDialog(null, ex.toString());
                }

                MusicGroupsAlbumArchive.albumTitle = null;//Μηδενίζουμε την τιμή για να μην σβηστεί κατά λάθος κάποια εγγραφή του πίνακα "Artist" πατώντας το κουμπί "Διαγραφή" κατά την εισαγωγή ενός νέου Καλλιτέχνη.
                new MusicGroupsAlbumArchive().setVisible(true);
                dispose();
            }    
        }
        
    }//GEN-LAST:event_SaveBtnActionPerformed

    private void CancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelBtnActionPerformed
        new MusicGroupsAlbumArchive().setVisible(true);
        dispose();
    }//GEN-LAST:event_CancelBtnActionPerformed

    private void InsertSongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertSongActionPerformed
        try{
            String Title = SongTitle.getText();
            String Duration = SongDuration.getText();
            int Number = Integer.parseInt(SongNumber.getText());
            song = new Song();//δημιουργία κάθε φορά μιας νέας οντότητας τύπου Song για να καταχωρηθεί στην λίστα τραγουδιών.
            song.setTitle(Title);
            song.setDuration(Duration);
            song.setTracknr(Number);
            
            //έλεγχος για την αποφυγή εγγραφής του ίδιου τραγουδιού στην λίστα με κριτήριο τον τίτλο του.
            songList.stream().forEach((songList1) -> {
                checkTitle = songList1.getTitle().equals(song.getTitle());
            });
            if (checkTitle == false){ 
               TableModel.addRow(new Object[]{SongTitle.getText(), SongDuration.getText(),Integer.parseInt(SongNumber.getText())});
               songList.add(song);
            }
             else
                 throw new Exception("Το τραγούδι υπάρχει στην λίστα!");
            
            //έλεγχος για την συμπλήρωση όλων των πεδίων.
            if(Title.length() == 0 || Duration.length() == 0 || Number == 0)
                throw new Exception("Όλα τα πεδία θα πρέπει να συμπληρωθούν");
            
            //μηδενισμός πεδίων
            SongTitle.setText(null);
            SongDuration.setText(null);
            SongNumber.setText(null);
            
            //τίθεται σε true για να επισυμανθεί η αλλαγή της λίστας τραγουδιών.
            flag = true;
            
        }
        
        catch(Exception e){
            JOptionPane.showMessageDialog(new JFrame(), e.getMessage());
        }
    }//GEN-LAST:event_InsertSongActionPerformed

    private void DeleteSongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteSongActionPerformed
        selectedRow = SongsTable.getSelectedRow();
        song = songList.get(SongsTable.convertRowIndexToModel(selectedRow));
        //αφαίρεση τραγουδιού από την λίστα τραγουδιών.
        songList.remove(song);
        //αφαίρεση τραγουδιού από τον πίνακα τραγουδιών.
        TableModel.removeRow(selectedRow);
        
        //τίθεται σε true για να επισυμανθεί η αλλαγή της λίστας τραγουδιών.
        flag = true;
    }//GEN-LAST:event_DeleteSongActionPerformed

   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox AlbumTypeCombo;
    private javax.swing.JButton CancelBtn;
    private javax.swing.JComboBox CompanyNameTxt3;
    private javax.swing.JTextField DiskNumberTxt2;
    private javax.swing.JComboBox GroupNameTxt5;
    private javax.swing.JButton InsertSong;
    private javax.persistence.EntityManager Radiostation_ProjectPUEntityManager;
    private javax.swing.JTextField ReleaseDateTxt4;
    private javax.swing.JButton SaveBtn;
    private javax.swing.JLabel Song;
    private javax.swing.JTextField SongDuration;
    private javax.swing.JTextField SongNumber;
    private javax.swing.JTextField SongTitle;
    private javax.swing.JTable SongsTable;
    private javax.swing.JTextField TitleTxt0;
    private java.util.List<Radiostation_POJOS.Artist> artistList;
    private javax.persistence.Query artistQuery;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private java.util.List<Radiostation_POJOS.Musicproductioncompany> musicproductioncompanyList;
    private javax.persistence.Query musicproductioncompanyQuery;
    private java.util.List<Radiostation_POJOS.Song> songList;
    private java.util.List<Radiostation_POJOS.Song> songList1;
    private javax.persistence.Query songQuery;
    private javax.persistence.Query songQuery1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
